<?php
    /**
    * Demand edit/add new
    * @author: trungdt
    * @since: Feb 2, 2011
    * @version: 1.0
    * 
    * Change logs
    * Feb 2, 2011 - trungdt - first create
    */

    if (!defined("EXPONENT")) exit("");
    $demand = null;
    $loc=null;
    $loc->mod="demandmodule";
    if (isset($_GET['id'])) {
        $demand = $db->selectObject("demands","id=".intval($_GET['id']));
        // this module dont use location_data
        //if ($listing != null) {
        //	$loc = unserialize($listing->location_data);
        //} 
    }

    // this module need administrator permission to manage subjects
    //if (exponent_permissions_check("manage",$loc)) {
    if (exponent_users_isLoggedIn() ){
        //$config = $db->selectObject('praymodule_config',"");
        //if ($config == null) {
        //	$config->item_per_page=10;
        //}

        if (($demand!=null) 
        && !(exponent_permissions_check("manage",$loc) 
            || exponent_permissions_check("approve_demand",$loc)))
        {
            echo SITE_404_HTML;
        }
        else
        {
            $form = demands::form($demand);
            //$form->location($loc);
            $form->meta("action","demand_save");
            // because we dont set $loc, we must set module name
            $form->meta("module","demandmodule");
            $form->meta("ajax_action",1);
            // this div is for ajax result
            $form->registerBefore("submit",null,null,new htmlcontrol("<div id='submit_result' class='submit_result'></div>"));
            $template = new template("demandmodule","_demand_edit",$loc);
            $template->assign("is_edit",(isset($demand->id) ? 1 : 0));
            $template->assign("form_html",$form->toHTML());
            $template->output();   
        }
    } else {
        // Feb 7, 2011 - trungdt - Change from Error 403 to login page
        $tlink=exponent_core_makeLink(array(
            'module'=>'loginmodule',
            'action'=>'loginredirect'));
        header("Location: {$tlink}");
    }

?>
